import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Semaphore;

/**
 * @author zengfh
 * @version 1.0
 * date 2018/04/24 16:15
 */
public class TestThread{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(TestThread.class);
    
    private static final CountDownLatch COUNT_DOWN_LATCH = new CountDownLatch(10000);
    
    private static final CyclicBarrier CYCLIC_BARRIER = new CyclicBarrier(300);
    
    private static final Semaphore SEMAPHORE = new Semaphore(30);
    
    
    /**
     * 人齐开会
     */
    static class CountDownLatchThread implements Runnable{
    
        private static int count = 0;
        
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
                //System.out.println(Thread.currentThread() + "线程开始");
                
                count = count + 1;
                System.out.println(count);
                COUNT_DOWN_LATCH.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            COUNT_DOWN_LATCH.countDown();
        }
    }
    
    
    /**
     * 坐满发车
     */
    static class CyclicBarrierThread implements Runnable{
    
        private static int count = 0;
    
        @Override
        public void run() {
            try {
                Thread.sleep(5000);
                System.out.println(Thread.currentThread() + " cyclicBarrier start");
                count += 1;
                System.out.println(count);
                CYCLIC_BARRIER.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 限流
     */
    static class SemaphoreThread implements Runnable{
    
        private static int count = 0;
    
        @Override
        public void run() {
            try {
                Thread.sleep(5000);
                SEMAPHORE.acquire();
                
                System.out.println(Thread.currentThread() + " semaphore start " + System.currentTimeMillis() + " QueueLength" + SEMAPHORE.getQueueLength());
                SEMAPHORE.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    
    public static void main(String[] args) {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(10000);
        //for (int i = 0; i < 10000; i++) {
        //    executor.execute(new CountDownLatchThread());
        //}
    
        //for (int i = 0; i < 1000; i++) {
        //    executor.execute(new CyclicBarrierThread());
        //}
    
        for (int i = 0; i < 10000; i++) {
            executor.execute(new SemaphoreThread());
        }
    }
}
